package com.example.administrator.audioandvideo

import android.media.AudioFormat
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException

class PcmToWavUtil {
    /**
     * 缓存的音频大小
     */
    var mBufferSize: Int = 0

    /**
     * 采样率
     */
    var mSimpleRate: Int = 0

    /**
     * 声道数
     */
    var mChannel: Int = 0

    constructor(mBufferSize: Int, mSimpleRate: Int, mChannel: Int) {
        this.mBufferSize = mBufferSize
        this.mSimpleRate = mSimpleRate
        this.mChannel = mChannel
    }

    fun pcmToWav(inFileName: String, outFileName: String) {
        var longSampleRate = mSimpleRate.toLong()
        var channels = if (mChannel == AudioFormat.CHANNEL_IN_MONO) 1 else 2
        var byteRate = 16 * longSampleRate * channels / 8
        var fis = FileInputStream(inFileName)
        var fos = FileOutputStream(outFileName)
        var totalAudioLen = fis.channel.size()
        var totalDataLen = totalAudioLen + 36
        var data = ByteArray(mSimpleRate)
        writeWaveFileHeader(fos,totalAudioLen,totalDataLen,longSampleRate,channels,byteRate)
        while (fis.read(data)!=-1){
            fos.write(data)
        }
        fis.close()
        fos.close()
    }

    /**
     * 加入wav文件头
     */
    @Throws(IOException::class)
    private fun writeWaveFileHeader(out: FileOutputStream, totalAudioLen: Long,
                                    totalDataLen: Long, longSampleRate: Long, channels: Int, byteRate: Long) {
        val header = ByteArray(44)
        // RIFF/WAVE header
        header[0] = 'R'.toByte()
        header[1] = 'I'.toByte()
        header[2] = 'F'.toByte()
        header[3] = 'F'.toByte()
        header[4] = (totalDataLen and 0xff).toByte()
        header[5] = (totalDataLen shr 8 and 0xff).toByte()
        header[6] = (totalDataLen shr 16 and 0xff).toByte()
        header[7] = (totalDataLen shr 24 and 0xff).toByte()
        //WAVE
        header[8] = 'W'.toByte()
        header[9] = 'A'.toByte()
        header[10] = 'V'.toByte()
        header[11] = 'E'.toByte()
        // 'fmt ' chunk
        header[12] = 'f'.toByte()
        header[13] = 'm'.toByte()
        header[14] = 't'.toByte()
        header[15] = ' '.toByte()
        // 4 bytes: size of 'fmt ' chunk
        header[16] = 16
        header[17] = 0
        header[18] = 0
        header[19] = 0
        // format = 1
        header[20] = 1
        header[21] = 0
        header[22] = channels.toByte()
        header[23] = 0
        header[24] = (longSampleRate and 0xff).toByte()
        header[25] = (longSampleRate shr 8 and 0xff).toByte()
        header[26] = (longSampleRate shr 16 and 0xff).toByte()
        header[27] = (longSampleRate shr 24 and 0xff).toByte()
        header[28] = (byteRate and 0xff).toByte()
        header[29] = (byteRate shr 8 and 0xff).toByte()
        header[30] = (byteRate shr 16 and 0xff).toByte()
        header[31] = (byteRate shr 24 and 0xff).toByte()
        // block align
        header[32] = (2 * 16 / 8).toByte()
        header[33] = 0
        // bits per sample
        header[34] = 16
        header[35] = 0
        //data
        header[36] = 'd'.toByte()
        header[37] = 'a'.toByte()
        header[38] = 't'.toByte()
        header[39] = 'a'.toByte()
        header[40] = (totalAudioLen and 0xff).toByte()
        header[41] = (totalAudioLen shr 8 and 0xff).toByte()
        header[42] = (totalAudioLen shr 16 and 0xff).toByte()
        header[43] = (totalAudioLen shr 24 and 0xff).toByte()
        out.write(header, 0, 44)
    }
}